import * as actionTypes from './actionTypes'

// immutable 解决修改 state 的问题，他不改 state，set 方法会结合之前 immutable 对象的值和新值，并返回一个全新对象
import {
  fromJS
} from 'immutable'

// 把 js 对象 转化为 immutable 对象
const defaultState = fromJS({
  focused: false, // 控制搜索框 focuse 事件
  mouseIn: false, // 控制搜索面板 mouse 事件
  list: [], // 该数组已被转化为 immutable 数组
  page: 1,
  totalPage: 1
})

export default (state = defaultState, action) => {

  switch (action.type) {
    case actionTypes.SEARCH_FOCUS:
      return state.set('focused', true)

    case actionTypes.SEARCH_BLUR:
      return state.set('focused', false)

    case actionTypes.MOUSE_ENTER:
      return state.set('mouseIn', true)

    case actionTypes.MOUSE_LEAVE:
      return state.set('mouseIn', false)

    case actionTypes.CHANGE_PAGE:
      return state.set('page', action.newPage)

    case actionTypes.CHANGE_LIST:
      return state.merge({
        list: fromJS(action.data),  // 转化为 immutable 数组
        totalPage: action.totalPage
      })

    default:
      return state
  }
}